
// 方法注释  =>   /** */

/**
 *  设置cookie
 * @param {string} key 键名
 * @param {string} val 健值(如果健值中存在中文和特殊字符 会进行编码)
 * @param {number} expires  过期时间
 * @param {string} path 存储路径
 * @return undefined
 */
function setCookie(key, val, expires, path = "/") {
    if (expires == undefined) { // 不传参
        document.cookie = key + "=" + encodeURIComponent(val) + "; path=" + path;
    } else {
        var date = new Date();
        date.setSeconds(date.getSeconds() + expires);  // 7天后过期
        //  7天后过期  cookie存储在站点根目录下 /
        document.cookie = key + "=" + encodeURIComponent(val) + "; expires=" + date.toUTCString() + "; path=" + path;
    }
}

function getCookie(key) {
    var cookie = document.cookie;
    var obj = {};
    if(cookie){
        var list = cookie.split("; ");
        for(var i = 0;i<list.length;i++){
            var item = list[i];  // 'user=a123123'
            var name = item.split("=")[0];
            var val = item.split("=")[1];
            if(name == key){ // cookie解析的键名和取值键名一致
                return decodeURIComponent(val); // 取值时进行解码
            }
        }
    }
    return "";
}

function deleteCookie(key){
    setCookie(key,"",-1);
}